package controller;

import com.alibaba.fastjson.JSONArray;
import org.junit.jupiter.api.Test;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import pojo.LoginInformation;
import pojo.User;
import service.Login.LoginService;
import service.User.UserService;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * @author HZY
 */
@Controller
@RequestMapping("/loginService")
public class loginServiceController {
    @Resource
    public LoginService loginService;
    @RequestMapping("/get0")
    @ResponseBody
    public Object get0(){
        return "aA";
    }
    @RequestMapping("/get1")
    @ResponseBody
    public Object get1(){
        return Map.of("a","A");
    }
    @RequestMapping("/get2")
    @ResponseBody
    public Object get2(){
        return JSONArray.toJSONString("aA");
    }
    @RequestMapping("/get3")
    @ResponseBody
    public Object get3(){
        return JSONArray.toJSONString(Map.of("a","A"));
    }
    /**
     * 跳转到登入界面
     * @return 请求跳转到登入界面"index"
     */
    @RequestMapping("/loginView")
    public String getLoginView(){
        return "AccountView/Login";
    }
    /**
     * 跳转到主界面
     * @return 请求跳转到主界面"index"
     */
    @RequestMapping("/indexView")
    public String getIndexView(){
        return "index";
    }

    /**
     * 登入事务
     * @param TorE 用户输入的电话或邮箱
     * @param password 用户输入的密码
     * @param model 返回的模型 包含("User", user)或错误信息("error", "登入信息有误")
     * @return 请求跳转到"index"
     */
    @RequestMapping("/login")
    public String login(
            @RequestParam(value = "TorE") String TorE,
            @RequestParam(value = "password") String password,
            Model model) {
        //对象化登入信息
        LoginInformation loginInformation = new LoginInformation();
        loginInformation.setPassword(password);
        if (TorE.contains("@")) {
            loginInformation.setEmail(TorE);
        } else {
            loginInformation.setTelephone(TorE);
        }
        //获取登入
        User user = loginService.loginUser(loginInformation);
        //验证登入情况
        if (user == null) {
            model.addAttribute("error", "登入信息有误");
            return "error";
        }
        //登入成功
        model.addAttribute("my",user);
        return "forward:/userService/viewUser?UserId="+user.getId();
    }
    /**
     * 注册事务
     * @param username
     * @param TorE
     * @param password
     * @param model
     * @return
     */
    @RequestMapping("/Register")
    public String register(
            @RequestParam(value = "username") String username,
            @RequestParam(value = "TorE") String TorE,
            @RequestParam(value = "password") String password,
            Model model) {
        System.out.println(username);
        //对象化注册信息
        User newuser = new User();
        newuser.setUsername(username);
        if (TorE.contains("@")) {
            newuser.setEmail(TorE);
        } else {
            newuser.setTelephone(TorE);
        }
        //验证是否可以注册
        User user = loginService.getbackUser(newuser);
        if (user != null) {
            if (user.getTelephone() != null) {
                model.addAttribute("error", "该 号码 已注册");
            }
            if (user.getEmail() != null) {
                model.addAttribute("error", model.getAttribute("error") + "该 邮箱 已注册");
            }
            if (user.getUsername() != null) {
                model.addAttribute("error", model.getAttribute("error") + "该 用户名 已被使用");
            }
            return "error";
        }
        //进行注册
        Map<String,String> map=newuser.getMap(password);
        loginService.registerUser(map);
        newuser.setId(Integer.parseInt(String.valueOf( map.get("id"))));
        //注册通过填充默认值
        try {
            userServiceController.createUserInformation(newuser.getId());
        } catch (IOException e) {
            e.printStackTrace();
        }
        //注册成功自动登入
        model.addAttribute("my",newuser);
        return "forward:/userService/viewUser?UserId="+newuser.getId();
    }
    /**
     * 账号找回事务
     * @param TorE
     * @param model
     * @return
     */
    @RequestMapping("/getbackUser")
    public String getbackUser(
            @RequestParam(value = "TorE") String TorE,
            Model model){
        User user=new User();
        if (TorE.contains("@")) {
            user.setEmail(TorE);
        } else {
            user.setTelephone(TorE);
        }
        //进行找回
        User rUser = loginService.getbackUser(user);
        //验证找回
        if (rUser==null){
            System.out.println("error" + "该 用户 不存在");
            return "error";
        }
        //找回成功获得原密码，用户信息
        String userpassword = loginService.getUserpassword(rUser.getId());
        model.addAttribute("userpassword",userpassword);
        model.addAttribute("my",rUser);
        return "forward:/userService/viewUser?UserId="+rUser.getId();
    }
    /**
     * 生成验证码
     * @param model
     */
    @RequestMapping("/getVerificationCode")
    public void getVerificationCode(Model model){
        Random random=new Random();
        String cod="";
        //生成5位随机的验证码
        for (int i=0;i<5;i++){
            cod+=(char)(random.nextInt(56)+65);
        }
        System.out.println(cod);
        model.addAttribute("VerificationCode",cod);
    }
//发送验证码
}
